﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WxStockWinSvc.Models.Entities;
using WxStockWinSvc.DALs.Http;
using WxStockWinSvc.Models.Enums;
using System.Data;
using WxStockWinSvc.DALs.Dbs;
using WxStockWinSvc.Commons;
using System.Threading;
using DX.CRC.Log;

namespace WxStockWinSvc.BIZs
{
    class BizLoadStock163
    {
        internal static void LoadStocksFrom163()
        {
            List<StockInfo4Load> stockCodes = DalWxStock.LoadStockBaseInfos();

            List<List<StockInfo4Load>> stocksColl = new List<List<StockInfo4Load>>();
            int iStockSum = stockCodes.Count;
            int iThreadCount = CommonUtil.LoadStockThreadCount;
            for (int i = 0; i < iThreadCount; i++)
            {
                int iTakeSum = i == iThreadCount - 1
                    ? stockCodes.Count
                    : iStockSum / iThreadCount;

                stocksColl.Add(stockCodes.Take(iTakeSum).ToList());
                stockCodes.RemoveRange(0, iTakeSum);
            }

            foreach (List<StockInfo4Load> stockInfos4Load in stocksColl)
            {
                Action<List<StockInfo4Load>> action = LoadStocks;
                AsyncCallback callback = delegate(IAsyncResult result) { action.EndInvoke(result); };
                action.BeginInvoke(stockInfos4Load, callback, null);
            }
        }

        /// <summary>
        /// 【西西 2012-12-25-233523】批量获取多个Stock信息
        /// </summary>
        /// <param name="stockInfos"></param>
        static void LoadStocks(List<StockInfo4Load> stockInfos)
        {
            foreach (StockInfo4Load stockCode in stockInfos)
                LoadSpecifiedStock4AllYears(stockCode);
        }

        /// <summary>
        /// 【西西 2012-12-25-233443】只获取基本的一条Stock数据
        /// </summary>
        /// <param name="stockInfo">stockCode</param>
        static void LoadSpecifiedStock4AllYears(StockInfo4Load stockInfo)
        {
            List<string> listStr = Dal163StockData.LoadStockInfoSpecifyDate(stockInfo
                , DateTime.Now.AddDays(1));

            InsertWxStock(stockInfo.ExchangeCode, GenerateDataTable(listStr));
        }

        static void InsertWxStock(ChinaExchangeCategory exCategory, DataTable dtConstruct)
        {
            while (dtConstruct.Rows.Count > 0)
            {
                DalWxStock.InsertWxStock(exCategory, dtConstruct.xTake(CommonUtil.QoCollOnceCount));
                dtConstruct.xRemoveRange(CommonUtil.QoCollOnceCount);
            }
        }

        static DataTable GenerateDataTable(List<string> listResult)
        {
            DataTable dtResult = new DataTable();

            //日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值

            for (int i = 0; i < listResult.Count; i++)
            {
                string[] sValues = listResult[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (i == 0)
                    foreach (string str in sValues)
                        dtResult.Columns.Add(str);
                else
                    dtResult.Rows.Add(sValues);
            }

            return dtResult;
        }
    }
}



//foreach (DataRow dr in dtResult.Rows)
//{
//    CommonStock163 stock = new CommonStock163();

//    stock.StockCode = dr["股票代码"].ToString().Replace("'", string.Empty);
//    stock.StockName = dr["名称"].ToString();
//    stock.Date = Convert.ToDateTime(dr["日期"]);

//    stock.CeilingPrice = ConstructValue(dr["最高价"]);
//    stock.ClosePrice = ConstructValue(dr["收盘价"]);
//    stock.FloatPercent = ConstructValue(dr["涨跌幅"]);
//    stock.FloatPrice = ConstructValue(dr["涨跌额"]);
//    stock.FloorPrice = ConstructValue(dr["最低价"]);
//    stock.LastClosePrice = ConstructValue(dr["前收盘"]);
//    stock.OpenPrice = ConstructValue(dr["开盘价"]);
//    stock.TurnOverRate = ConstructValue(dr["换手率"]);
//    stock.VolumnAmount = ConstructValue(dr["成交金额"]);

//    stock.MarketCap = ConstructValue(dr["总市值"]);
//    stock.MarketCapCirculate = ConstructValue(dr["流通市值"]);
//    stock.VolumnCount = ConstructValue(dr["成交量"]);

//    list.Add(stock);
//    i++;
//}

//static decimal? ConstructValue(object drValue)
//{
//    if (drValue == DBNull.Value)
//        return null;
//    else
//        return Convert.ToDecimal(drValue);
//}